<?php
/**
 * Created by PhpStorm.
 * User: JYuans
 * Date: 2019/12/6
 * Time: 16:41
 */
namespace Bstage\Controller;

class TeamExamineController extends BstageController {
    /**
     * 班子考评列表查询
     */
    public function teamExamineQuery(){
        //分页
        $now_page = $_REQUEST['now_page']?$_REQUEST['now_page']:1;//默认第一页
        $page_size = $_REQUEST['page_size']?$_REQUEST['page_size']:50;//默认每页50条
        //查询条件
        $where = array();
        //筛选姓名
        if($_REQUEST["search_title"]){
            $where['title'] = ['like','%'.$_REQUEST['search_title'].'%'];
        }

        $field = "team_examine_id,status,title,down_start_time,down_end_time,same_start_time,same_end_time,up_start_time,up_end_time,add_time";
        $list_data = M('TeamExamine')
            ->where($where)
            ->field($field)
            ->order('add_time DESC')
            ->page($now_page,$page_size)
            ->select();

        if(!$list_data) {
            $this->returnJson(503,'没有更多数据');
        }

        $status_str_array = ["未完成设置","未开始","进行中","已结束"];
        foreach($list_data as $key=>$value){
            $list_data[$key]['status_str'] = $status_str_array[$value['status']];

            $list_data[$key]['down_start_time'] = changeDate($value['down_start_time']);
            $list_data[$key]['down_end_time'] = changeDate($value['down_end_time']);

            $list_data[$key]['same_start_time'] = changeDate($value['same_start_time']);
            $list_data[$key]['same_end_time'] = changeDate($value['same_end_time']);

            $list_data[$key]['up_start_time'] = changeDate($value['up_start_time']);
            $list_data[$key]['up_end_time'] = changeDate($value['up_end_time']);

            $list_data[$key]['add_time'] = changeDateTime($value['add_time']);
        }

        $count = M('TeamExamine')
            ->where($where)
            ->count();

        $return_data = array();
        $return_data['list_data'] = $list_data;
        $return_data['count'] = $count;
        $return_data['now_page'] = $now_page;
        $return_data['page_size'] = $page_size;
        $return_data['max_page'] = ceil($count / $page_size);

        $this->successReturn("获取成功",$return_data);
    }

    /**
     * 添加接口
     */
    public function addTeamExamineAction(){
        $new_data = array();
        $new_data['title'] = $_REQUEST['title']?$_REQUEST['title']:$this->errorReturn("请输入班子考评标题");
        $new_data['down_start_time'] = $_REQUEST['down_start_time']?strtotime($_REQUEST['down_start_time']):$this->errorReturn("请输入员工考评时间，开始时间");
        $new_data['down_end_time'] = $_REQUEST['down_end_time']?strtotime($_REQUEST['down_end_time'])+86399:$this->errorReturn("请输入员工考评时间，结束时间");

        $new_data['same_start_time'] = $_REQUEST['same_start_time']?strtotime($_REQUEST['same_start_time']):$this->errorReturn("请输入中级考评时间，开始时间");
        $new_data['same_end_time'] = $_REQUEST['same_end_time']?strtotime($_REQUEST['same_end_time'])+86399:$this->errorReturn("请输入中级考评时间，结束时间");

        $new_data['up_start_time'] = $_REQUEST['up_start_time']?strtotime($_REQUEST['up_start_time']):$this->errorReturn("请输入高管考评时间，开始时间");
        $new_data['up_end_time'] = $_REQUEST['up_end_time']?strtotime($_REQUEST['up_end_time'])+86399:$this->errorReturn("请输入高管考评时间，结束时间");

        $new_data['item_json'] = "[]";
        $new_data['employing_item_json'] = "[]";

        $new_data['add_time'] = time();

        $add_result = M("TeamExamine")
            ->add($new_data);

        if(!$add_result){
            $this->errorReturn("添加班子考评失败，请重新尝试");
        }

        $this->successReturn("添加成功",['team_examine_id'=>$add_result]);
    }

    /**
     * 修改信息接口
     */
    public function editTeamExamineAction(){
        $team_examine_id = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");

        $new_data = array();
        $new_data['title'] = $_REQUEST['title']?$_REQUEST['title']:$this->errorReturn("请输入班子考评标题");
        $new_data['down_start_time'] = $_REQUEST['down_start_time']?strtotime($_REQUEST['down_start_time']):$this->errorReturn("请输入员工考评时间，开始时间");
        $new_data['down_end_time'] = $_REQUEST['down_end_time']?strtotime($_REQUEST['down_end_time'])+86399:$this->errorReturn("请输入员工考评时间，结束时间");

        $new_data['same_start_time'] = $_REQUEST['same_start_time']?strtotime($_REQUEST['same_start_time']):$this->errorReturn("请输入中级考评时间，开始时间");
        $new_data['same_end_time'] = $_REQUEST['same_end_time']?strtotime($_REQUEST['same_end_time'])+86399:$this->errorReturn("请输入中级考评时间，结束时间");

        $new_data['up_start_time'] = $_REQUEST['up_start_time']?strtotime($_REQUEST['up_start_time']):$this->errorReturn("请输入高管考评时间，开始时间");
        $new_data['up_end_time'] = $_REQUEST['up_end_time']?strtotime($_REQUEST['up_end_time'])+86399:$this->errorReturn("请输入高管考评时间，结束时间");

        $new_data['update_time'] = time();

        $update_result = M("TeamExamine")
            ->where(['team_examine_id'=>$team_examine_id])
            ->save($new_data);

        if(!$update_result){
            $this->errorReturn("更新班子考评信息失败，请重新尝试");
        }

        $this->successReturn("修改成功");
    }

    /**
     * 获取数据接口
     */
    public function getTeamExamineAction(){
        $team_examine_id = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");

        $field = "title,down_start_time,down_end_time,same_start_time,same_end_time,up_start_time,up_end_time";
        $field .= ",item_json,employing_item_json";

        $examine_data = M("TeamExamine")
            ->where(['team_examine_id'=>$team_examine_id])
            ->field($field)
            ->find();

        if(!$examine_data){
            $this->errorReturn("班子考评ID错误，请重新尝试");
        }

        $examine_data['down_start_time'] = changeDate($examine_data['down_start_time']);
        $examine_data['down_end_time'] = changeDate($examine_data['down_end_time']);
        $examine_data['same_start_time'] = changeDate($examine_data['same_start_time']);
        $examine_data['same_end_time'] = changeDate($examine_data['same_end_time']);
        $examine_data['up_start_time'] = changeDate($examine_data['up_start_time']);
        $examine_data['up_end_time'] = changeDate($examine_data['up_end_time']);

        $examine_data['item_array'] = json_decode($examine_data['item_json'],true);
        unset($examine_data['item_json']);
        $examine_data['employing_item_array'] = json_decode($examine_data['employing_item_json'],true);
        unset($examine_data['employing_item_json']);

        $examine_data['new_p_array'] = M("TeamExamineNewPromotion")
            ->alias("tenp")
            ->join("__TEAM__ t on tenp.team_id = t.team_id")
            ->where(['tenp.team_examine_id'=>$team_examine_id])
            ->field("tenp.new_p_num,tenp.team_id,t.name as team_name,tenp.name,tenp.date_birth,tenp.former_duties,tenp.now_duties,tenp.appointment_time")
            ->select();
        foreach($examine_data['new_p_array'] as $key=>$value){
            $examine_data['new_p_array'][$key]['date_birth'] = date("Y-m",$value['date_birth']);
            $examine_data['new_p_array'][$key]['appointment_time'] = changeDateTime($value['appointment_time']);
        }

        $this->successReturn("获取成功",$examine_data);
    }

    /**
     * 删除接口
     */
    public function deleteTeamExamineAction(){
        $team_examine_id = $_REQUEST['id_str'] ? $_REQUEST['id_str'] : $this->errorReturn("缺少班子考评ID");

        $delete_result = M("TeamExamine")
            ->where(['team_examine_id'=>['in',$team_examine_id]])
            ->delete();

        if(!$delete_result){
            $this->errorReturn("删除班子考评失败，请重新尝试");
        }

        $this->successReturn("删除成功");
    }

    /**
     * 修改状态接口
     */
    public function setExamineStatus(){
        $team_examine_id = $_REQUEST['team_examine_id'] ? $_REQUEST['team_examine_id'] : $this->errorReturn("缺少班子考核ID");
        $status = $_REQUEST['status']?$_REQUEST['status']:$this->errorReturn("缺少状态参数");

        $check_time = M("TeamExamine")
            ->where(['team_examine_id'=>$team_examine_id])
            ->field("status")
            ->find();

        if($status == 1){       //从未设置状态 修改为 未开始
            //检查考核状态是否为未设置
            if($check_time['status'] != 0) {
                $this->errorReturn("无法多次进入未开始状态");
            }
            $update_result = M("TeamExamine")
                ->where(['team_examine_id'=>$team_examine_id])
                ->setField("status",1);
            if(!$update_result){
                $this->errorReturn("修改状态失败");
            }
        }else if($status == 2){    //从未开始状态 修改为 进行中
            if($check_time['status'] != 1){
                $this->errorReturn("该考核已结束或未设置完成，请重新尝试");
            }
            $update_result = M("TeamExamine")
                ->where(['team_examine_id'=>$team_examine_id])
                ->setField("status",2);
            if(!$update_result){
                $this->errorReturn("修改状态失败");
            }
        }else if($status == 3){     //从进行中状态 修改 已结束
            if($check_time['status'] != 2){
                $this->errorReturn("该考核未开始或已结束，无法进行结束操作，请重新尝试");
            }
            $update_result = M("TeamExamine")
                ->where(['team_examine_id'=>$team_examine_id])
                ->setField("status",3);
            if(!$update_result){
                $this->errorReturn("修改状态失败");
            }
        }else{
            $this->errorReturn("传入状态值错误");
        }

        $this->successReturn("修改成功");
    }

    /**
     * 班子考核详情首页接口
     */
    public function TeamExamineInfo(){
        $team_examine_id = $_REQUEST['team_examine_id'] ? $_REQUEST['team_examine_id'] : $this->errorReturn("缺少班子考核ID");

        $return_array = array();
        $return_array['down_comprehensive'] = 0;
        $return_array['down_comprehensive_complete'] = 0;
        $return_array['down_employing'] = 0;
        $return_array['down_employing_complete'] = 0;
        $return_array['down_new_p'] = 0;
        $return_array['down_new_p_complete'] = 0;

        $return_array['same_comprehensive'] = 0;
        $return_array['same_comprehensive_complete'] = 0;
        $return_array['same_employing'] = 0;
        $return_array['same_employing_complete'] = 0;
        $return_array['same_new_p'] = 0;
        $return_array['same_new_p_complete'] = 0;

        $return_array['up_comprehensive'] = 0;
        $return_array['up_comprehensive_complete'] = 0;
        $return_array['up_employing'] = 0;
        $return_array['up_employing_complete'] = 0;
        $return_array['up_new_p'] = 0;
        $return_array['up_new_p_complete'] = 0;

        $relation_down = M("TeamExamineRelationDown")
            ->where(['team_examine_id'=>$team_examine_id])
            ->group("examine_user_id")
            ->field("comprehensive_status,employing_status,new_p_status")
            ->select();
        foreach($relation_down as $key=>$value){
            $return_array['down_comprehensive']++;
            if($value['comprehensive_status'] == 1) {
                $return_array['down_comprehensive_complete']++;
            }
            $return_array['down_employing']++;
            if($value['employing_status'] == 1) {
                $return_array['down_employing_complete']++;
            }
            $return_array['down_new_p']++;
            if($value['new_p_status'] == 1) {
                $return_array['down_new_p_complete']++;
            }
        }

        $relation_same = M("TeamExamineRelationSame")
            ->where(['team_examine_id'=>$team_examine_id])
            ->group("examine_user_id")
            ->field("comprehensive_status,employing_status,new_p_status")
            ->select();
        foreach($relation_same as $key=>$value){
            $return_array['same_comprehensive']++;
            if($value['comprehensive_status'] == 1) {
                $return_array['same_comprehensive_complete']++;
            }
            $return_array['same_employing']++;
            if($value['employing_status'] == 1) {
                $return_array['same_employing_complete']++;
            }
            $return_array['same_new_p']++;
            if($value['new_p_status'] == 1) {
                $return_array['same_new_p_complete']++;
            }
        }

        $relation_up = M("TeamExamineRelationUp")
            ->where(['team_examine_id'=>$team_examine_id])
            ->group("examine_user_id")
            ->field("comprehensive_status,employing_status,new_p_status")
            ->select();
        foreach($relation_up as $key=>$value){
            $return_array['up_comprehensive']++;
            if($value['comprehensive_status'] == 1) {
                $return_array['up_comprehensive_complete']++;
            }
            $return_array['up_employing']++;
            if($value['employing_status'] == 1) {
                $return_array['up_employing_complete']++;
            }
            $return_array['up_new_p']++;
            if($value['new_p_status'] == 1) {
                $return_array['up_new_p_complete']++;
            }
        }

        $this->successReturn("获取成功",$return_array);
    }
}